import React from 'react';
import { Button, Modal } from 'antd';
import WFLeftOrgTree from './WFLeftOrgTree';
import {
  WeaTop,
  WeaTab,
  WeaRightMenu,
  WeaPopoverHrm,
  WeaLeftRightLayout,
  WeaLocaleProvider,
} from 'ecCom';
import CarConstant from '../../util/CarConstant';
import { WeaTableNew } from 'comsMobx';

const WeaTable = WeaTableNew.WeaTable;
import { inject, observer } from 'mobx-react';
import NoRight from '../NoRight';
import CreateCarUseWorkflowDialog from '../car/dialog/CreateCarUseWorkflowDialog';
import AddCarUseWorkflowDialog from '../car/dialog/AddCarUseWorkflowDialog';
import EditCarUseWorkflowDialog from '../car/dialog/EditCarUseWorkflowDialog';

const getLabel = WeaLocaleProvider.getLabel;
@inject('carUseCarWorkflowSetStore', 'useCarWorkflowStore', 'routing')
@observer
export default class CarUseCarWorkflowSet extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      subCompanyId: 0,
      workflowDetachable: 0,
      subCompanyName: '',
      isRight: 0,
    };
  }

  componentWillMount() {
    const { carUseCarWorkflowSetStore } = this.props;
    carUseCarWorkflowSetStore.getWorkflowDetachable(this.setWorkflowDetachable);// 获取分权信息
  }

	setWorkflowDetachable = (result) => {
	  if (result) {
	    this.setState({
	      workflowDetachable: result.detachable && result.detachable == 1 ? 1 : 0,
	      isRight: result.isRight == 1 ? 1 : 0,
	    });
	  }
	}

	componentDidMount() {
	  const { carUseCarWorkflowSetStore } = this.props;
	  carUseCarWorkflowSetStore.subCompanyId = 0;
	  carUseCarWorkflowSetStore.doTableLoading(); // 初始化列表
	  // carUseCarWorkflowSetStore.initConditionDatas(); //初始化高级搜索区域
	}

	render() {
	  const { carUseCarWorkflowSetStore, useCarWorkflowStore, routing } = this.props;

	  const { loading, topTab, sessionkey, tableStore, setQueckSearch, subCompanyId } = carUseCarWorkflowSetStore;
	  const { isRight } = carUseCarWorkflowSetStore;
	  const { workflowDetachable } = this.state;
	  if (isRight == 1) {
	    const table = () => (<div className="wea-right-container">
	      <WeaTab
    ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@qoc3hp`}
    datas={topTab}
	        //	keyParam="groupid"
	        //	onChange={ }
	        //	selectedKey={ selectedKey }
	        searchType={['base']}
	        //	searchsBaseValue={  }
	        //	setShowSearchAd={ bool => { carFlowListStore.setShowSearchAd(bool) } }
	        //	showSearchAd={ showSearchAd }
	        //	hideSearchAd={ () => { carFlowListStore.setShowSearchAd(false) } }
	        //	searchsAd={ CarUtil.getSearchs(form ,conditioninfo) }
	        //	buttonsAd={ this.getTabButtonsAd() }
    onSearchChange={(v) => { setQueckSearch({ queckSearch: v, subCompanyId }); }}
    onSearch={(v) => { carUseCarWorkflowSetStore.doTableLoading(v); }}
  />
	      <WeaPopoverHrm ecId={`${this && this.props && this.props.ecId || ''}_WeaPopoverHrm@pgwpbh`} />

	      <WeaTable
	        ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@rloyn6`}
	        loading={loading}
	        // #819764 使用toJS() WeaTable组件出错
	        // comsWeaTableStore={toJS(tableStore)}
    comsWeaTableStore={tableStore}
    sessionkey={sessionkey}
    hasOrder
    needScroll={false}
    getColumns={this.getNewColumns}
    onOperatesClick={this.onOperatesClick}
  />
	    </div>
	    );


	    return (
	      <div className="wea-car-mine">
	        <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@538zxi`} datas={this.getRightMenu()} onClick={this.onRightMenuClick.bind(this)}>
    {/* <Spin spinning={spinning}> */}
	          <WeaTop
	            ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@tq4xqw`}
    title={getLabel(126428, '用车流程设置')}
	            loading={loading}
    icon={CarConstant.icon}
    iconBgcolor={CarConstant.iconBgColor}
    buttonSpace={10}
    showDropIcon
	            dropMenuDatas={this.getRightMenu()}
	            onDropMenuClick={this.onRightMenuClick.bind(this)}
	            buttons={[
	              (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@th2m4i@onCreateButtonClick`} type="primary" onClick={() => this.onCreateButtonClick()}>{getLabel(82, '新建')}</Button>),
	              (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@o9qge9@onAddButtonClick`} type="ghost" onClick={() => this.onAddButtonClick()}>{getLabel(611, '添加')}</Button>)]}
  >


    {
	              workflowDetachable === 1 ?
	                (
	                  <WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@xwz8r5`} isNew leftCom={<WFLeftOrgTree ecId={`${this && this.props && this.props.ecId || ''}_WFLeftOrgTree@prw30g`} changeSubcompany={this.changeSubcompany.bind(this)} />} >
	                    {table()}
  </WeaLeftRightLayout>
	                ) : (
	                  table()
	                )
	            }

  </WeaTop>
    {/* </Spin> */}
  </WeaRightMenu>
    <CreateCarUseWorkflowDialog ecId={`${this && this.props && this.props.ecId || ''}_CreateCarUseWorkflowDialog@wj2wvm`} currentStore={useCarWorkflowStore} parentStore={carUseCarWorkflowSetStore} routing={routing} ref="createCarUseWorkflow" />
	        <AddCarUseWorkflowDialog ecId={`${this && this.props && this.props.ecId || ''}_AddCarUseWorkflowDialog@8peuqf`} currentStore={useCarWorkflowStore} parentStore={carUseCarWorkflowSetStore} ref="addCarUseWorkflow" />

	        {useCarWorkflowStore.editDialogVisible && <EditCarUseWorkflowDialog ecId={`${this && this.props && this.props.ecId || ''}_EditCarUseWorkflowDialog@50thn7`} currentStore={useCarWorkflowStore} parentStore={carUseCarWorkflowSetStore} ref="editCarUseWorkflow" />}
  </div>

	    );
	  }
	  return <NoRight ecId={`${this && this.props && this.props.ecId || ''}_NoRight@0ic9xs`} visible />;
	}

	changeSubcompany = (nodeObj) => {
	  let id = '';
	  // let name = '';
	  if (nodeObj && nodeObj.id && nodeObj.id > 0) {
	    id = nodeObj.id;
	    // name = nodeObj.name;
	  }
	  this.setState({ subCompanyId: nodeObj.id, subCompanyName: nodeObj.name });
	  const { carUseCarWorkflowSetStore } = this.props;
	  carUseCarWorkflowSetStore.subCompanyId = id;
	  carUseCarWorkflowSetStore.showLoading(true);
	  carUseCarWorkflowSetStore.doTableLoading();
	}

	// 右键菜单
	getRightMenu() {
	  const btns = [];
	  btns.push({ key: 'create', icon: <i className="icon-coms-New-Flow" />, content: getLabel(82, '新建') });
	  btns.push({ key: 'add', icon: <i className="icon-coms-AddTo" />, content: getLabel(611, '添加') });

	  // btns.push({ key: "collection", icon: <i className='icon-coms-Custom'/>, content: '收藏' });
	  // btns.push({ key: "help", icon: <i className='icon-coms-Custom'/>, content: '帮助' });
	  return btns;
	}

	// 右键菜单事件绑定
	onRightMenuClick(key) {
	  if (key == 'create') {
	    this.onCreateButtonClick();
	  } else if (key == 'add') {
	    this.onAddButtonClick();
	  }
	}
	// 新建按钮
	onCreateButtonClick = () => {
	  this.refs.createCarUseWorkflow.open(this.state);
	}
	// 添加按钮
	onAddButtonClick = () => {
	  this.refs.addCarUseWorkflow.open({ id: 0 });
	}
	// 编辑按钮
	onEditButtonClick = (param) => {
	  const { useCarWorkflowStore } = this.props;
	  // this.refs.editCarUseWorkflow.open({id:param.id});
	  useCarWorkflowStore.carbasicid = param.id;
	  useCarWorkflowStore.workflowid = param.wid;
	  useCarWorkflowStore.editDialogVisible = true;
	}

	reloadTable = () => {
	  const { carUseCarWorkflowSetStore } = this.props;

	  carUseCarWorkflowSetStore.doTableLoading(); // 初始化列表
	}

	onOperatesClick = (record, index, operate, flag) => {
	  const { useCarWorkflowStore } = this.props;
	  const wid = record.workflowid;
	  const id = record.randomField2;
	  if (flag == 0) { // 启用
	    useCarWorkflowStore.operation = 'use';
	    useCarWorkflowStore.id = id;
	    useCarWorkflowStore.doSubmit({ id }, this.reloadTable);
	  } else if (flag == 1) { // 禁用
	    useCarWorkflowStore.operation = 'close';
	    useCarWorkflowStore.id = id;
	    useCarWorkflowStore.workflowid = wid;
	    useCarWorkflowStore.doSubmit({ id }, this.reloadTable);
	  } else if (flag == 2) { // 编辑
	    // var url = "/cpt/car/UseCarWorkflowSetAddTab.jsp?dialog=1&id="+id;
	    // window.openFullWindowHaveBar(url);

	    this.onEditButtonClick({ id, wid });
	  } else if (flag == 3) { // 流程设置流程设置
	    // window.parent.location = "/workflow/workflow/addwf.jsp?ajax=1&src=editwf&wfid="+id+"&isTemplate=0";
	    const url = `/workflowengine/path/pathSet/pathDetail/baseSet/baseInfo?workflowId=${wid}&showLeftTree=false`;
	    this.props.routing.push(url);
	  } else if (flag == 4) { // 删除
	    const confirm = Modal.confirm;
	    const reloadTable = this.reloadTable;
	    confirm({
	      title: getLabel(15097, '确定要删除吗?'),
	      content: getLabel(15097, '确定要删除吗?') + getLabel(385844, '删除后不可恢复！'),
	      onOk() {
	        useCarWorkflowStore.operation = 'delete';
	        useCarWorkflowStore.id = id;
	        useCarWorkflowStore.doSubmit({ id }, reloadTable);
	      },
	      onCancel() { },
	      reloadTable,
	    });
	    // var str = "<%=SystemEnv.getHtmlLabelName(15097,user.getLanguage())%>";
	    // Dialog.confirm(
	    // 	str,
	    // 	function(){
	    // 		document.frmmain.action="UseCarWorkflowSetOperation.jsp?operation=delete&id="+id;
	    // 		document.frmmain.submit();
	    // 	},
	    // 	function(){
	    // 		return;
	    // 	},
	    // 	200,
	    // 	80
	    // );
	  }
	}
	// penWorkflowEditPage =(workflowid) =>{
	// 	var url = "/workflow/workflow/addwf.jsp?src=editwf&wfid="+workflowid+"&isTemplate=0";
	// 	openFullWindowHaveBar(url);
	// }
}

// export const openWorkflowEditPage =(workflowid) =>{
// 	var url = "/workflow/workflow/addwf.jsp?src=editwf&wfid="+workflowid+"&isTemplate=0";
// 	openFullWindowHaveBar(url);
// }

/**
 * 打开弹出窗口
 */
export const openFullWindowHaveBar = (url) => {
  const redirectUrl = url;
  const width = screen.availWidth - 10;
  const height = screen.availHeight - 50;
  let szFeatures = 'top=0,';
  szFeatures += 'left=0,';
  szFeatures += `width=${width},`;
  szFeatures += `height=${height},`;
  szFeatures += 'directories=no,';
  szFeatures += 'status=yes,toolbar=no,location=no,';
  szFeatures += 'menubar=no,';
  szFeatures += 'scrollbars=yes,';
  szFeatures += 'resizable=yes';
  window.open(redirectUrl, '', szFeatures);
};

window.openFullWindowHaveBar = openFullWindowHaveBar;
